package utils
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;

	public class LoaderManage
	{
		private static var instance:LoaderManage;
		
		private var loader:Loader;
		
		private var urlReq:URLRequest;
		
		private var callback:Function;
		
		public function LoaderManage()
		{
			if(instance)throw new Error("Singerleton!");
		}
		
		public static function getInstance():LoaderManage{
			return instance ||=new LoaderManage();
		}
		
		public function loaderImager(url:String,complete:Function):void{
			callback = complete;
			loader = new Loader();
			urlReq = new URLRequest(url+"?="+Math.random()*10000);
			loader.load(urlReq);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderComplete);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,io_error);
		}
		
		private function io_error(event:IOErrorEvent):void
		{
			GameUtils.destoyEvent(event);
			loader =null;
			urlReq = null;
		}
		
		private function loaderComplete(event:Event):void
		{
			GameUtils.destoyEvent(event);
			loader =null;
			urlReq = null;
			callback(event.target.content);
		}
		
	}
}